#ifndef _RF_H_
#define _RF_H_

#include "Global.h"

#define P_RF_OUT    RB6
#define M_RF_OUT    TRISB6

// #define P_RF_OUT    RB4
// #define M_RF_OUT    TRISB4


#define _RF_FORMATE1_   (0)
#define _RF_FORMATE2_   (0)
#define _RF_FORMATE3_   (1)



//------------------- Э�� 1 ----------------------//

#if _RF_FORMATE1_

#define C_TM_RF_BIT_SYNC        5
#define C_TM_RF_BIT_SYNC_0      5
#define C_TM_RF_BIT_SYNC_1      15
#define C_TM_RF_BIT_0           15
#define C_TM_RF_BIT_1           5
#define C_TM_RF_HEAD_H          15
#define C_TM_RF_HEAD_L          140
#define C_TM_RF_FRAME_SYNC_H    5
#define C_TM_RF_FRAME_SYNC_L    5
#define C_TM_DELAY_FRAME        1

typedef struct
{
    uint8 FanTemp:3;        // ����
    uint8 Hydraulic:3;      // ˮѹ
    uint8 Reserved:1;
    uint8 PowerSave:1;

}Typedef_RF_DATA2;

typedef struct
{
    uint8 WaterTemp:3;
    uint8 SeatTemp:3;
    uint8 Resvered:2;
}Typedef_RF_DATA3;

typedef struct
{
    uint8 Position:4;
    uint8 Resvered:4;
}Typedef_RF_DATA4;

typedef struct
{
    uint8 KeyCount:4;
    uint8 CSKeyCount:4;
}Typedef_RF_DATA5;


typedef struct
{
    uint16 ID;
    uint8  Key;
    Typedef_RF_DATA2 Data2;
    Typedef_RF_DATA3 Data3;
    Typedef_RF_DATA4 Data4;
    Typedef_RF_DATA5 Data5;

    uint8  CS;
}Typedef_RF_Pack;

#endif


//------------------- Э�� 2 ----------------------//
#if _RF_FORMATE2_

#define C_TM_RF_BIT_SYNC        5
#define C_TM_RF_BIT_SYNC_0      5
#define C_TM_RF_BIT_SYNC_1      15
#define C_TM_RF_BIT_0           15
#define C_TM_RF_BIT_1           5
#define C_TM_RF_HEAD_H          5
#define C_TM_RF_HEAD_L          140
#define C_TM_RF_FRAME_SYNC_H    5
#define C_TM_RF_FRAME_SYNC_L    5
#define C_TM_DELAY_FRAME        1

typedef struct
{
    uint8 WaterTemp:3;
    uint8 Reserved:5;
}Typedef_RF_DATA2;

typedef struct
{
    uint8 FanTemp:3;
    uint8 Reserved:5;
}Typedef_RF_DATA3;

typedef struct
{
    uint8 SeatTemp:3;
    uint8 Reserved:4;
    uint8 RemoteFlg:1;
}Typedef_RF_DATA4;

typedef struct
{
    uint16 ID;
    uint8  Key;
    Typedef_RF_DATA2 Data2;
    Typedef_RF_DATA3 Data3;
    Typedef_RF_DATA4 Data4;
    uint8 CS;
}Typedef_RF_Pack;
#endif              

//------------------- 协议3 ----------------------//
#if _RF_FORMATE3_
#define C_TM_RF_BASE            50
#define C_TM_RF_HEAD_H          (2700ul/C_TM_RF_BASE)
#define C_TM_RF_HEAD_L          (1200ul/C_TM_RF_BASE)
#define C_TM_RF_BIT_SYNC_0      (400ul/C_TM_RF_BASE)
#define C_TM_RF_BIT_SYNC_1      (800ul/C_TM_RF_BASE)
#define C_TM_RF_BIT_0           (400ul/C_TM_RF_BASE)
#define C_TM_RF_BIT_1           (400ul/C_TM_RF_BASE)
#define C_TM_RF_END_H           (1200ul/C_TM_RF_BASE)
#define C_TM_RF_END_L           (1200ul/C_TM_RF_BASE)
// #define C_TM_RF_FRAME_SYNC_H    5
// #define C_TM_RF_FRAME_SYNC_L    5
// #define C_TM_DELAY_FRAME        1

typedef struct 
{
    uint8 ShuiWen:2;
    uint8 FengWen:2;
    uint8 ZuoWen:2;
    uint8 Reversed:2;
}Typedef_PARA;

typedef struct
{
    uint16 ID;
    uint8  UserCode;
    uint8  Key;
    Typedef_PARA Para;
    uint8 CS;
}Typedef_RF_Pack;
#endif


typedef union
{
    Typedef_RF_Pack Detail;
    uint8 Buffer[sizeof(Typedef_RF_Pack)];
}U_RF_PACK;

#define C_REPEAT_TIMES      2

#define C_MAX_RF_SYNC_BIT   16
#define C_MAX_RF_SEND_BIT   (sizeof(Typedef_RF_Pack)*8)
#define C_RF_DLY_BASE    8
extern const uint8	TAB_BIT_RF[];

extern U_RF_PACK   RF_Pack;
extern uint8       RF_RemindSendTimes;
extern uint8       RF_SendSetp;
extern uint8       RF_DelayCnt;
extern uint8       RF_SendCount;
enum
{
    C_RF_SEND_SYNC_H=0,
    C_RF_SEND_SYNC_L,
    C_RF_SEND_HEAD_H,
    C_RF_SEND_HEAD_L,
    C_RF_SEND_DATA_H,
    C_RF_SEND_DATA_L,
    C_RF_SEND_END_H,
    C_RF_SEND_END_L,
    C_RF_SEND_FINISH,
};


void    initRF(void);
void    RF_Send(uint8 key);

void RF_SendTest(void);


#endif

